-
-
Notifications
You must be signed in to change notification settings - Fork 345
fix: infer type of undefinable parameters correctly #1397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughIntroduces IsSkipped type-based gating across context and schema merge utilities, adds new utility types (IsUnknown, IsUndefined, IsSkipped), updates merge logic to use IsSkipped, and adds a new type builder Undefinable to the type-system with a public alias t.Undefinable. Adds type-only tests verifying Undefinable inference. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
Tip 👮 Agentic pre-merge checks are now available in preview!Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.
Please see the documentation for more information. Example: reviews:
pre_merge_checks:
custom_checks:
- name: "Undocumented Breaking Changes"
mode: "warning"
instructions: |
Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal). Please share your feedback with us on this Discord post. Pre-merge checks (3 passed)✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/types.ts (1)
667-667
: Replace legacy undefined-check with IsSkipped for cookie mergeundefined extends A['cookie'] incorrectly treats unions like X | undefined as "skipped"; align with other fields by using IsSkipped.
Location: src/types.ts:667
- cookie: undefined extends A['cookie'] ? B['cookie'] : A['cookie'] + cookie: IsSkipped<A['cookie']> extends true ? B['cookie'] : A['cookie']
🧹 Nitpick comments (3)
test/types/index.ts (3)
2685-2689
: Avoid runtime access of "~Routes"; use type-leveltypeof
insteadAccessing
app["~Routes"].post...
is a value-level read and can throw if executed. Prefer the established pattern usingtypeof
so this remains purely type-checked.- expectTypeOf(app["~Routes"].post.query).not.toEqualTypeOf<unknown>(); - expectTypeOf(app["~Routes"].post.query).toEqualTypeOf<{ name: string } | undefined>(); - expectTypeOf(app["~Routes"].post.headers).toEqualTypeOf<{ token: string } | undefined>(); - expectTypeOf(app["~Routes"].post.body).toEqualTypeOf<{ id: number } | undefined>(); + expectTypeOf<(typeof app)['~Routes']['post']['query']>().not.toBeUnknown() + expectTypeOf<(typeof app)['~Routes']['post']['query']>().toEqualTypeOf<{ name: string } | undefined>() + expectTypeOf<(typeof app)['~Routes']['post']['headers']>().toEqualTypeOf<{ token: string } | undefined>() + expectTypeOf<(typeof app)['~Routes']['post']['body']>().toEqualTypeOf<{ id: number } | undefined>()
2685-2689
: Optional: assert headers/body are notunknown
for completenessMirror the query assertion to ensure headers/body don’t regress to
unknown
.+ expectTypeOf<(typeof app)['~Routes']['post']['headers']>().not.toBeUnknown() + expectTypeOf<(typeof app)['~Routes']['post']['body']>().not.toBeUnknown()
2677-2683
: Stylistic nits: align quotes/semicolons with file conventionsMost of this file uses single quotes and omits trailing semicolons. Consider matching that for consistency.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
src/context.ts
(3 hunks)src/type-system/index.ts
(2 hunks)src/types.ts
(4 hunks)test/types/index.ts
(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/context.ts (1)
src/types.ts (2)
IsSkipped
(753-753)ResolvePath
(239-249)
🔇 Additional comments (9)
src/type-system/index.ts (2)
583-583
: Public alias looks good.Exposing as
t.Undefinable
keeps parity with existing helpers.
422-427
: Clarify Undefinable semantics & verify OpenAPI/JSON Schema handling of TypeBox UndefinedFile: src/type-system/index.ts (lines ~422-427)
- Update the doc comment: this builder unions with Undefined only and does NOT mark properties optional. Suggested diff:
/** - * Allow Optional and Undefined + * Allow Undefined (does NOT mark properties optional) */ Undefinable: <T extends TSchema>(schema: T, options?: SchemaOptions) => t.Union([schema, t.Undefined()], options),
- Verify your schema→OpenAPI/JSON Schema emitter tolerates TypeBox's Undefined inside unions (or document/transform it). If no emitter exists in this repo, state where conversion happens so compatibility can be checked.
src/types.ts (4)
657-660
: Move to IsSkipped for body/headers/query merge — good.This fixes the “undefined extends T” pitfall and preserves unions like
T | undefined
.
682-703
: Standalone merge for body/headers/query updated correctly.Symmetric
IsSkipped
branching reads well.
710-717
: Cookie standalone merge aligned — good.This further highlights the inconsistency in MergeSchema’s cookie; fix there as noted.
745-754
: Helper types look correct and non-distributive.
IsUnknown
excludesany
and matches only exactunknown
.IsUndefined
checks exactundefined
via tuple wrapping.IsSkipped
composition is clear.src/context.ts (3)
15-17
: Importing IsSkipped for gating — good.Keeps types local and avoids repeating logic here.
33-51
: ErrorContext gating now respects unknown/exact-undefined — good.Safer defaults for query/headers/cookie while preserving typed branches when provided.
126-146
: Context query/params/headers/cookie gating updated correctly.Behavior now matches the new inference strategy; params fallback handling still covers path-derived types.
Addresses #1395
The main cause of the problem is,
undefined extends T
is true forT = undefined | Something
so any type that had that signature was being resolved to the default type.I added proper
IsUndefined
check. Also noticed that it should also fallback to the default type when the type is unknown, so I addedIsUnknown
check. Then, I merged them in theIsSkipped
check so that can be used generally for these cases. If the condition to fallback to the default type changes in the future, it will be enough to change the implementation ofIsSkipped
.Finally, added a
t.Undefinable
helper.t.Optional
would have been a better name but Typebox uses it for making a schema partial.t.MaybeEmpty
also includesnull
type, and that makes this syntax not possible:I know the changes I made are somewhat risky and intrusive. And I know you are working on integrating standard-schema. So feel free to modify or discard this PR. But I think a change like this is needed, as there is no way to have a type that is unioned with undefined at the moment.
Note: A similar change is also needed for Eden, and I am planning to also make that if this is merged. Because the same problem exists there too.
Summary by CodeRabbit
New Features
Improvements
Tests